//
//  LiveCatogryViewController.m
//  penzer
//
//  Created by caiyc on 2018/9/25.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "LiveCatogryViewController.h"
#import "SKTagView.h"
#import "TagView.h"
#import "FilterCell.h"
#import "FilterTag.h"
@interface LiveCatogryViewController ()<TagViewDelegate,UITableViewDelegate,UITableViewDataSource,filtertagDelegate>
@property(nonatomic,strong)TagView *tagView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)FilterCell *cell;
@property(nonatomic,strong)NSMutableArray *slectBtn_Arr;
@property(nonatomic,strong)NSMutableArray *tag_Arr;
@property(nonatomic,strong)NSMutableArray *tag_Height;
@property(nonatomic,strong)NSDictionary *filterDic;
@property(nonatomic,strong)NSMutableDictionary *fileterDics;

@end

@implementation LiveCatogryViewController
-(NSMutableArray *)tag_Height{
    if(!_tag_Height){
        _tag_Height= [NSMutableArray array];
    }
    return _tag_Height;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation:1 andTitle:@"筛选" andNaviColor:nil andTitleColor:nil];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.fileterDics = [NSMutableDictionary dictionary];
 //   [self configData];
  //  [self layoutViews];
  [self loadData];
 //   [self layoutView];
  
    // Do any additional setup after loading the view from its nib.
}
-(void)loadData{
     NSDictionary *paramter = @{@"op":@"member",@"ctrl":@"live",@"ac":@"member",@"ta":@"screen"};
    [self weqPostHandle:paramter success:^(id response) {
        if(response){
            self.filterDic = response[@"data"];
            [self configData];
           
            [self.fileterDics setValue:[NSString stringWithFormat:@"%@",response[@"data"][@"activity_cate"][0][@"id"]] forKey:@"activity_cate"];

                [self.fileterDics setValue:[NSString stringWithFormat:@"%@",response[@"data"][@"product_cate"][0][@"id"]] forKey:@"product_cate"];

                [self.fileterDics setValue:[NSString stringWithFormat:@"%@",response[@"data"][@"sex"][0][@"id"]] forKey:@"sex"];

                [self.fileterDics setValue:[NSString stringWithFormat:@"%@",response[@"data"][@"level"][0][@"id"]] forKey:@"level"];

                [self.fileterDics setValue:[NSString stringWithFormat:@"%@",response[@"data"][@"age"][0][@"id"]] forKey:@"age"];

                [self.fileterDics setValue:[NSString stringWithFormat:@"%@",response[@"data"][@"game"][0][@"id"]] forKey:@"game"];
          
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)layoutViews{

    self.confirm_Btn.layer.cornerRadius = 15;
    self.confirm_Btn.layer.masksToBounds =1;
    self.ScroVuews.backgroundColor = [UIColor whiteColor];
    self.ScroVuews.showsVerticalScrollIndicator = 0;
    CGFloat tagview_H = 70;
    CGFloat tagview_W = self.ScroVuews.frame.size.width;
    for(int i =0;i<self.dataSource.count;i++){
       // UIView *botomView = [UIView alloc]initWithFrame:CGRectMake(0, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);
        UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, tagview_H, screen_width, 40)];
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 200, 40)];
        NSString *titles = @"直播形式";
        if(i==1){
            titles = @"直播类型";
        }else if (i==2){
            titles = @"性别限制";
        }else if (i==3){
            titles = @"等级限制";
        }else if (i==4){
            titles = @"年龄限制";
        }else if(i==5){
            titles = @"游戏模式";
        }
        title.text = titles;
        title.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        [titleView addSubview:title];
        title.tag = i+1024;
        [self.ScroVuews addSubview:titleView];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(screen_width-60, 0, 40, 40);
        [btn setImage:[UIImage imageNamed:@"downs"] forState:UIControlStateNormal];
         [btn setImage:[UIImage imageNamed:@"up"] forState:UIControlStateSelected];
        [titleView addSubview:btn];
        btn.tag = i;
        [btn addTarget:self action:@selector(shouqi:) forControlEvents:UIControlEventTouchUpInside];
        tagview_H+=40;
        FilterTag *tagviews =[[FilterTag alloc]initWithFrame:CGRectMake(0, 0, tagview_W, 0)];
        [tagviews setArr:self.dataSource[i]];
        tagviews.tag = i;
        tagviews.delegate = self;
      //  [self.ScroVuews addSubview:tagviews];
        [self.tag_Arr addObject:tagviews];
        [ self.tag_Height addObject:[NSString stringWithFormat:@"%f",tagviews.frame.size.height]];
        [self.slectBtn_Arr addObject:btn];
        
        tagview_H+=tagviews.frame.size.height;
    }
    self.ScroVuews.contentSize = CGSizeMake(0, tagview_H+300);
}
-(void)shouqi:(UIButton *)sender{
    sender.selected = !sender.selected;
    NSInteger index = sender.tag;
    TagView *tags = [self.tag_Arr objectAtIndex:index];
  //  CGFloat height = tags.frame.size.height;
    CGFloat height = tags.frame.size.height;
     for(UIView *view in [tags subviews])
     {
         [view removeFromSuperview];
     }
    for(int k=index+1;k<self.dataSource.count;k++){
        TagView *tag = self.tag_Arr[k];
        UILabel *labe = (UILabel *)[self.ScroVuews viewWithTag:1024+k];
        labe.frame = CGRectMake(labe.frame.origin.x, labe.frame.origin.y-height, 200, 40);
        CGRect oldRect = tag.frame;
        tag.frame = CGRectMake(oldRect.origin.x, oldRect.origin.y-height, oldRect.size.width, oldRect.size.height);
    }
}
-(void)handleSelectTag:(NSString *)keyWord andTag:(NSInteger)index AndPindex:(NSInteger)pindex{
    
    NSLog(@"文字：%@,元素索引：%ld，父元素索引:%ld",keyWord,(long)index,(long)pindex);
    NSString *ids=self.dataSource[pindex][@"datas"][index][@"id"];
    if(pindex==0){
        [self.fileterDics setValue:ids forKey:@"activity_cate"];
    }else if (pindex==1){
         [self.fileterDics setValue:ids forKey:@"product_cate"];
    }else if (pindex==2){
         [self.fileterDics setValue:ids forKey:@"sex"];
    }else if (pindex==3){
         [self.fileterDics setValue:ids forKey:@"level"];
    }else if (pindex==4){
         [self.fileterDics setValue:ids forKey:@"age"];
    }else{
         [self.fileterDics setValue:ids forKey:@"game"];
    }
}
-(void)configData{

    NSDictionary *dic1 = @{@"title":@"直播形式",@"datas":self.filterDic[@"activity_cate"]};
    NSDictionary *dic2 = @{@"title":@"直播类型",@"datas":self.filterDic[@"product_cate"]};
    NSDictionary *dic3 = @{@"title":@"性别限制",@"datas":self.filterDic[@"sex"]};
    NSDictionary *dic4 = @{@"title":@"等级限制",@"datas":self.filterDic[@"level"]};
    NSDictionary *dic5 = @{@"title":@"年龄限制",@"datas":self.filterDic[@"age"]};
    NSDictionary *dic6 = @{@"title":@"游戏模式",@"datas":self.filterDic[@"game"]};
    [self.dataSource addObject:dic1];
    [self.dataSource addObject:dic2];
    [self.dataSource addObject:dic3];
    [self.dataSource addObject:dic4];
    [self.dataSource addObject:dic5];
    [self.dataSource addObject:dic6];
    
    CGFloat tagview_W = self.ScroVuews.frame.size.width;
    for(int i =0;i<self.dataSource.count;i++){
        // UIView *botomView = [UIView alloc]initWithFrame:CGRectMake(0, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);
      
        FilterTag *tagviews =[[FilterTag alloc]initWithFrame:CGRectMake(0, 0, tagview_W, 0)];
        [tagviews setArr:self.dataSource[i][@"datas"]];
        tagviews.tag = i;
        tagviews.delegate = self;
        //  [self.ScroVuews addSubview:tagviews];
        [self.tag_Arr addObject:tagviews];
        [ self.tag_Height addObject:[NSString stringWithFormat:@"%f",tagviews.frame.size.height]];

    }
    
    NSLog(@"---%@",self.tag_Height);
    NSLog(@"---%@",self.tag_Arr);
    [self.tableView reloadData];
   // [self.dataSource addObject:@"鞋包"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    UIButton *btn = (UIButton *)[self.slectBtn_Arr objectAtIndex:section];
 //   btn.selected = !btn.selected;
    if(btn.selected==1){
        return 0;
    }else
        return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectZero];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_width, 50)];
    titleView.backgroundColor = [UIColor whiteColor];
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 200, 50)];
    title.text = self.dataSource[section][@"title"];
    title.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    [titleView addSubview:title];
   // title.tag = i+1024;
    [self.ScroVuews addSubview:titleView];
    
    if(self.slectBtn_Arr.count>6){
        UIButton *btnds= (UIButton *)[self.slectBtn_Arr objectAtIndex:section];
        [titleView addSubview:btnds];
    }else{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(screen_width-60, 0, 40, 40);
    [btn setImage:[UIImage imageNamed:@"downs"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"up"] forState:UIControlStateSelected];
    [titleView addSubview:btn];
    btn.tag = section;
    [btn addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:btn];
    [self.slectBtn_Arr addObject:btn];
    }
    return titleView;
}
-(void)close:(UIButton *)sender{
    UIButton *btn = (UIButton *)[self.slectBtn_Arr objectAtIndex:sender.tag];
    btn.selected = !btn.selected;
    [self.slectBtn_Arr replaceObjectAtIndex:sender.tag withObject:btn];
    [self.tableView reloadData];
  //  [self.tableView reloadSections:[NSIndexPath indexPathWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationTop];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
       }
    for(UIView *ve in [cell.contentView subviews])
    {
        [ve removeFromSuperview];
    }
    [cell.contentView addSubview:self.tag_Arr[indexPath.section]];
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UIButton *btn = (UIButton *)[self.slectBtn_Arr objectAtIndex:indexPath.section];
    if(btn.selected==1){
        return 0;
    }else
    return [self.tag_Height[indexPath.section]floatValue];
}
-(void)layoutView{
    // 先移除掉所有
    self.tagView.arr = self.dataSource;

    [self.view addSubview:self.tagView];
   
}
-(TagView *)tagView{
    if (!_tagView) {
        _tagView = [[TagView alloc]initWithFrame:CGRectMake(0,KISIphoneX?100:80 , screen_width, 0)];
        _tagView.delegate = self;
        
    }
    return _tagView;
}
-(NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource=[NSMutableArray array];
    }
    return _dataSource;
}
-(NSMutableArray *)tag_Arr{
    if(!_tag_Arr){
        _tag_Arr = [NSMutableArray array];
    }
    return _tag_Arr;
}
-(NSMutableArray *)slectBtn_Arr{
    if(!_slectBtn_Arr){
        _slectBtn_Arr = [NSMutableArray array];
    }
    return _slectBtn_Arr;
}
//-(NSMutableDictionary *)filterDic{
//    if(!_filterDic){
//        _filterDic = [NSMutableDictionary dictionaryWithCapacity:10000];
//    }
//    return _filterDic;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)confirm:(UIButton *)sender {
     self.filter(self.fileterDics);
    [self.navigationController popViewControllerAnimated:1];
   
}

@end
